﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using RegisterationCommon.Common;
using RegisterationCommon.Education;

namespace RegisterationSystem.Space_Education
{
    public partial class education_account : System.Web.UI.Page
    {
        public string Sid;
        public string EduSuperAccount;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Sid =Cookies.Get("ID").ToString();
                EduSuperAccount = Cookies.Get("EduSuperAccount").ToString();
                switch (Request.Form["action"])
                {
                    case "del":
                        DelSchoolacc();
                        break;
                    case "upd":
                        UpdSchoolacc();
                        break;
                    case "delacc":
                        DelSchoolaccByID();
                        break;

                }
            }
        }
        // 根据ID删除教育局帐号
        void DelSchoolaccByID()
        {
            string id = Request.Form["id"];
            EduAccount edu = new EduAccount();
            edu.ID = Convert.ToInt32(id);
           bool flag=edu.DelEduAccount();
           if (flag)
           {
               EduAccount.AddEduSysLog(Cookies.Get("AccountName").ToString(), DateTime.Now, HttpContext.Current.Request.UserHostAddress, "删除账号", "账号删除成功");
           }
           else
           {
               EduAccount.AddEduSysLog(Cookies.Get("AccountName").ToString(), DateTime.Now, HttpContext.Current.Request.UserHostAddress, "删除账号", "账号删除失败");
           }
        }
        //修改教育局帐号状态
        void UpdSchoolacc()
        {
            string id = Request.Form["id"];
            string status = Request.Form["status"];
            EduAccount edu = new EduAccount();
            edu.ID = Convert.ToInt32(id);
            edu.IsDisable = Convert.ToBoolean(Convert.ToInt32(status));
            bool flag = edu.MdfEduAccountStatus();
            if (flag)
            {
                EduAccount.AddEduSysLog(Cookies.Get("AccountName").ToString(), DateTime.Now, HttpContext.Current.Request.UserHostAddress, "修改账号", "账号修改成功");
            }
            else
            {
                EduAccount.AddEduSysLog(Cookies.Get("AccountName").ToString(), DateTime.Now, HttpContext.Current.Request.UserHostAddress, "修改账号", "账号修改失败");
            }
        }
        //删除教育局帐号
        void DelSchoolacc()
        {
            //获取删除学校帐号集合
            string idlist = Request.Form["id"];
            idlist = idlist.Substring(0, idlist.Length - 1);
            bool flag = DBAction.Delete("EduAccount", "id in (" + idlist + ")");
            if (flag)
            {
                EduAccount.AddEduSysLog(Cookies.Get("AccountName").ToString(), DateTime.Now, HttpContext.Current.Request.UserHostAddress, "删除账号", "账号删除成功");
            }
            else
            {
                EduAccount.AddEduSysLog(Cookies.Get("AccountName").ToString(), DateTime.Now, HttpContext.Current.Request.UserHostAddress, "删除账号", "账号删除失败");
            }
        }

        //添加
        protected void btnadd_Click(object sender, EventArgs e)
        {
            DataTable dt = DBAction.GetDTFromSQL("select * from EduAccount where AccountName='" + txtname.Value.Trim() + "'");
            if (dt.Rows.Count > 0)
            {
                Message.Send(10035);
                return;
            }
            EduAccount edu = new EduAccount();
            edu.AccountName = txtname.Value.Trim();
            edu.AccountPswd = BaseLibrary.ConvertToMD5(txtpwd.Value.Trim().ToLower());
            edu.IsDisable = ddlstatus.Value == "0" ? false : true;
            edu.IsSuper = true;
            edu.RoleID = 0;
            edu.SuperAccount =Convert.ToInt32(this.EduAccountType.Value);
            if (edu.AddEduAccount())
            {
                Message.Send(10033);
                EduAccount.AddEduSysLog(Cookies.Get("AccountName").ToString(), DateTime.Now, HttpContext.Current.Request.UserHostAddress, "添加账号", "账号添加成功");
            }
            else
            {
                Message.Send(10034);
                EduAccount.AddEduSysLog(Cookies.Get("AccountName").ToString(), DateTime.Now, HttpContext.Current.Request.UserHostAddress, "添加账号", "账号添加失败");
            }
        }

    }
}